<?

require('twitter.php');

class notificationController {
	private $smses_so_far = 0;
	private $enable_sms = 1; //KILL SWITCH
	
	function generateMessage($match) {
		$red1 = $match->getTeam('red',1);
		$red2 = $match->getTeam('red',2);
		$red3 = $match->getTeam('red',3);
		$blue1 = $match->getTeam('blue',1);
		$blue2 = $match->getTeam('blue',2);
		$blue3 = $match->getTeam('blue',3);
		$redscore = $match->getRedScore();
		$bluescore = $match->getBlueScore();
		$matchname = $match->getName();
		$eventname = $match->getEventName();
		
		if ($redscore > $bluescore) $victoryverb = "beat";
		if ($redscore < $bluescore) $victoryverb = "lost to";
		if ($redscore == $bluescore) $victoryverb = "tied";

		return "$red1, $red2, $red3 " . $victoryverb . " $blue1, $blue2, $blue3 " . "[$redscore to $bluescore] in " . $matchname . " at " . $eventname;
	}
	
	function tweet($match) {
		$twitter_message = $this->generateMessage($match);
		
		//This file sets $username and $password with the notification twitter account.
		include "twitter_credentials.php";
		$twitter = new twitter($username, $password);
						
		if(strlen($twitter_message)<1){
			$error=1; 
		} else { 
			$twitter_status = $twitter->setStatus($twitter_message); 
		}
		
		unset($twitter);
		echo "TWEET<br />";
	}
	
	function smsByTeams($match) {
		if ($this->enable_sms == 1) {
			$red1 = $match->getTeam('red',1);
			$red2 = $match->getTeam('red',2);
			$red3 = $match->getTeam('red',3);
			$blue1 = $match->getTeam('blue',1);
			$blue2 = $match->getTeam('blue',2);
			$blue3 = $match->getTeam('blue',3);
			$redscore = $match->getRedScore();
			$bluescore = $match->getBlueScore();
			$matchname = $match->getName();
			$event = $match->getEventID();
			
			$filter = "SELECT address FROM sms_notifications WHERE (team = '$red1' OR team = '$red2' OR team = '$red3' OR team = '$blue1' OR team = '$blue2' OR team = '$blue3' OR team = '-1') AND (event = '$event' OR event = '-1') AND verified = 1 AND active = 1";
			echo $filter . "<br />";
			$query = mysql_query($filter);
			
			$list_of_addresses = array();
			
			while ($address = mysql_fetch_array($query)) {
				$list_of_addresses[] = $address["address"];
			}
			
			//filter out duplicate addresses.
			$list_of_addresses = array_unique($list_of_addresses);
			
			//send messages.
			echo "TEXTING<br />";
			echo "<ul>";		
			$message = $this->generateMessage($match);
			$subject = "TBA";
			foreach ($list_of_addresses as $address) {
				//send message
				echo "<li>" . $address . "</li>";
			    mail($address, $subject, $message,
			        "From: The Blue Alliance<texter@thebluealliance.net>\nX-Mailer: PHP/" . phpversion());
			    $this->incrementSMS();
			}
			echo "</ul>";			
		} else {
			echo "SMS Notification Disabled.";
		}
		echo "<p>" . $this->smses_so_far . " text messages sent so far.</p>";
	}
	
	function incrementSMS() {
		$this->smses_so_far = $this->smses_so_far + 1;
	}

}

?>